<template>
  <ContainerGroup style="padding: 0 12px">
    <!-- filterForm -->
    <!-- <Container>
    </Container> -->
    <!-- el-table -->
    <!-- <Container>
      <FilterForm
        ref="filterForm"
        :elements="filterElements"
        :button-list="buttonList"
      />
    </Container> -->
    <Container class="table-box">
      <!-- <el-button
        type="primary"
        style="margin:12px 0 0 12px;background-color:#217dfa"
      >
        一键还原
      </el-button> -->
      <el-button
        style="margin: 12px 0 0 12px"
        :loading="loading"
        @click="restore"
      >
        非集管转集管
      </el-button>

      <el-button style="margin: 12px 0 0 12px" @click="changePop">
        集管转非集管
      </el-button>
      <div class="table-box">
        <el-table ref="elTable" :data="tabledata">
          <el-table-column align="center" type="selection" width="100px" />
          <el-table-column align="center" prop="bedNo" label="床位编号" />
          <el-table-column align="center" prop="deptName" label="科室" />
          <el-table-column align="center" prop="nurseCellName" label="病区" />
          <el-table-column align="center" prop="operReason" label="原因" />
          <el-table-column align="center" prop="createdOn" label="修改日期" />
          <el-table-column align="center" prop="operName" label="操作人" />
          <!-- <el-table-column align="center" label="修改前状态">
            <template slot-scope="scope">
              {{ scope.row.afterType === '1' ? '集管' : '非集管' }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="修改后状态">
            <template slot-scope="scope">
              {{ scope.row.beforeType === '1' ? '集管' : '非集管' }}
            </template>
          </el-table-column> -->
        </el-table>
      </div>
      <!-- <pagination
        :total="total"
        :page.sync="pageInfo.pageNum"
        :limit.sync="pageInfo.pageSize"
        @pagation="pagationChange"
      /> -->
      <el-pagination
        background
        :current-page="currentPage"
        :page-sizes="[10, 20, 30, 50]"
        :page-size="20"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="
          (e) => {
            pagationChange(e, 'size');
          }
        "
        @current-change="
          (e) => {
            pagationChange(e, 'current');
          }
        "
      />
      <Dialog
        title="集管转非集管"
        :visible="dialogVisible"
        :footer-btns="[
          {
            text: '确定',
            type: 'primary',
            trigger: 'confirm',
          },
          {
            text: '关闭',
            type: 'info',
            trigger: 'cancel',
          },
        ]"
        @cancel="dialogVisible = false"
        @confirm="dialogConfirm"
      >
        <div class="form-wraper">
          <span class="title-h">*注: 如不选病区,则默认为全部</span>
          <div class="form-item">
            <span class="form-item-label">病区：</span>
            <el-select
              v-model="areaValue"
              placeholder="请选择"
              size="mini"
              style="width: 180px"
              filterable
              clearable
            >
              <el-option
                v-for="item in patientAreaList"
                :key="item.deptCode"
                :label="item.deptName"
                :value="item.deptCode"
              />
            </el-select>
          </div>
        </div>
      </Dialog>
    </Container>
  </ContainerGroup>
</template>

<script>
import pagination from "../../components/pagination/index.vue";
import {
  selectCwInformation,
  changeBedTypeAPI,
  getWardList,
  changeBedTypeByNurseCode,
} from "../../utils/apis/registrationApi";

export default {
  name: "QueryChangeBeds",
  components: {
    pagination,
  },
  data() {
    return {
      areaValue: "",
      patientAreaList: [],
      dialogVisible: false,
      buttonList: [
        {
          label: "查询",
          type: "primary",
          icon: "search",
          handlerClick: this.handerSearch,
        },
        {
          label: "重置",
          type: "info",
          icon: "reset",
          handlerClick: this.handlerReset,
        },
      ],
      filterElements: [
        {
          key: "updateTime",
          type: "DateTimePicker",
          mode: "datetimerange",
          label: "修改时间",
          value: "",
          width: 500,
        },
      ],
      tabledata: [],

      pageInfo: {
        pageSize: 20,
        pageNum: 1,
      },
      total: 0,
      loading: false,
      currentPage: 1,
    };
  },
  created() {},
  mounted() {
    this.getData();
    this.getAreaList();
  },
  methods: {
    getAreaList() {
      getWardList({
        yard: "",
      }).then((res) => {
        if (res.data.code === 200) {
          this.patientAreaList = res.data.data;
        }
      });
    },
    changePop() {
      this.dialogVisible = true;
    },
    dialogConfirm() {
      console.info(this.areaValue);

      changeBedTypeByNurseCode(this.areaValue).then((res) => {
        if (res.data.code === 200) {
          this.$message.success("操作成功");
          this.dialogVisible = false;
          this.getData();
        } else {
          this.$message.error("操作失败");
        }
      });
    },
    pagationChange(paylod, type) {
      if (type === "size") {
        this.pageInfo.pageSize = paylod;
      } else {
        this.pageInfo.pageNum = paylod;
      }
      this.getData();
    },
    getData() {
      // console.log('getData')
      const params = {
        pageSize: this.pageInfo.pageSize,
        pageIndex: this.pageInfo.pageNum,
        // bedNo: '',
        // operName: '',
        // deptName: '',
        // nurseCellName: '',
        // operReason: '',
        // createOn: ''
      };
      console.log(params);
      selectCwInformation(params).then((res) => {
        console.log("查询返回值", res);
        if (res.data.code === 200) {
          this.total = res.data.data.total;
          this.tabledata = res.data.data.list;
        }
      });
    },
    restore() {
      if (this.$refs.elTable.selection.length) {
        this.loading = true;
        const tempArr = [];
        this.$refs.elTable.selection.forEach((item) => {
          const tempObj = {
            bedNo: item.bedNo,
            deptCode: item.deptCode,
            deptName: item.deptName,
            nurseCellCode: item.nurseCellCode,
            nurseCellName: item.nurseCellName,
            operId: item.operId,
            operName: item.operName,
            operReason: item.operReason,
            remark: "还原",
            beforeType: "0",
            afterType: "1",
          };
          tempArr.push(tempObj);
        });
        const params = tempArr;
        changeBedTypeAPI(params).then(
          (res) => {
            console.log("修改返回值", res);
            if (res.data.data) {
              this.$message.success("修改成功");
              this.loading = false;
              this.getData();
              this.$refs.elTable.clearSelection();
            } else {
              this.loading = false;
              this.$message.error("修改失败");
              this.$refs.elTable.clearSelection();
            }
          },
          (err) => {
            if (err) {
              this.loading = false;
              console.log(err);
              this.$message.error(err.data.error);
              this.$refs.elTable.clearSelection();
            }
          }
        );
      } else {
        this.$message.warning("请选择待还原床位！");
      }
    },
  },
};
</script>

<style lang="less" scoped>
.form-wraper {
  margin-top: 30px;
  .title-h {
    font-size: 12px;
    color: red;
    margin-bottom: 15px;
    display: inline-block;
  }
  .form-item {
    display: flex;
    align-items: center;
  }
}
/deep/ .el-button {
  width: 120px;
  height: 32px;
  background-color: #217dfa;
  color: #fff;
  padding: 7px 14px;
  border: none;
  span {
    font-size: 14px !important;
  }
}
/deep/ .el-button:hover {
  box-shadow: 0px 0px 6px #217dfa;
}
.table-box {
  margin-top: 12px;
  padding: 0 !important;
}
.title {
  padding: 18px;
  border-top-left-radius: 8px;
  border-top-right-radius: 8px;
  font-size: 20px;
  font-weight: bold;
  color: #fff;
  background-color: #2165f9;
}
.table-box {
  padding: 12px;
}
/deep/ .el-pagination {
  padding: 20px 16px;
  text-align: right;
}
</style>
